//
//  socialCenter.m
//  share
//
//  Created by Jacob on 8/4/14.
//  Copyright (c) 2014 com.haomee. All rights reserved.
//

#import "SocialCenter.h"
#import "QQClient.h"
#import "SinaClient.h"
#import "Weixin.h"
#import "FaceBook.h"
#import "shareConfig.h"
#define SAFE_STRING(str) ([(str) length] ? (str) : @"")
@interface SocialCenter()
@property (nonatomic,strong) NSMutableDictionary *platFormDictionary;
@end
@implementation SocialCenter
+(instancetype)sharedInstance{
    static SocialCenter *_socialInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _socialInstance = [[SocialCenter alloc]init];
    });
    return _socialInstance;
}

+(void)loginindex:(int )index Success:(void (^)(PlatformUserMoidel *model))success
                error:(void (^)(NSString *message))errorBlock
{
    if (index == 1) {
        [[SocialCenter clientWithPlatForm:platformQQ] getUSerInfoSuccess:success error:errorBlock];
    }
    if (index == 2) {
        [[SocialCenter clientWithPlatForm:platformSina] getUSerInfoSuccess:success error:errorBlock];
    }
    if (index == 3) {
        [[SocialCenter clientWithPlatForm:platformFaceBook] getUSerInfoSuccess:success error:errorBlock];
    }
    if (index == 4) {
        [[SocialCenter clientWithPlatForm:platformWeiXin] getUSerInfoSuccess:success error:errorBlock];
    }

}
+(void)shareToPlayformWithIndex:(int)index andURL:(NSString*)url andWithTitle:(NSString*)title andWithMessage:(NSString*)info andImageURL:(NSString*)imageURL andWithLocalImage:(UIImage*)localImage andBlock:(void (^)(BOOL isSuccess))shareBlock{
    platformName platformname;
    if(index == 0){  //微信好友
        platformname = platformWeiXin;
    }else if(index == 1){  //微信朋友圈
        platformname = platformWXfriends;
    }else if(index == 2){  //新浪微博
        platformname = platformSina;
    }else if(index == 3){  //QQ
        platformname = platformQQ;
    }else if(index == 4){  //QQ空间
        platformname = platformQzone;
    }else if(index == 5)
    {
        platformname = platformFaceBook;
    }
    
    [[SocialCenter clientWithPlatForm:platformname] shareImage:localImage title:title description:info andImageUrl:imageURL andurl:url andCompletionBlock:^(BOOL isSuccess){
        NSLog(@"%@",isSuccess?@"分享成功":@"分享失败");
        shareBlock(isSuccess);
    }];
}

+(void)getFriendshipsAtIndex:(int )index completionBlock:(void(^)(BOOL isSuccess,NSArray *array))block
{
    if (index == 1) {
        [[SocialCenter clientWithPlatForm:platformQQ] getFriendships:block];
         }
    if (index == 2) {
       [[SocialCenter clientWithPlatForm:platformQzone] getFriendships:block];
    }
    if (index == 3) {
        [[SocialCenter clientWithPlatForm:platformFaceBook] getFriendships:block];
    }

}






+ (id<socialDateSource>)clientWithPlatForm:(platformName )platFormName
{
    return [[SocialCenter sharedInstance] clientWithPlatForm:platFormName];
}



- (id<socialDateSource>)clientWithPlatForm:(platformName)platForm
{
    if (platForm == platformSina) {
        return [SinaClient sharedInstance];
    }
    else if (platForm == platformQQ)
    {
        QQClient *qq = [QQClient sharedInstance];
        qq.isQzoneShare = NO;
        return qq;
    }
    else if (platForm == platformFaceBook)
    {
        return [FaceBook shareInstance];
    }
    else if (platForm == platformQzone)
    {
        QQClient *qq = [QQClient sharedInstance];
        qq.isQzoneShare = YES;
        return qq;
    }
    else if (platForm == platformWeiXin)
    {
        Weixin *wx = [Weixin sharedInstance];
        wx.isFriend = NO;
        return wx;
    }
    else if (platForm == platformWXfriends)
    {
        Weixin *wx = [Weixin sharedInstance];
        wx.isFriend = YES;
        return wx;
    }

    
    return nil;
}

+ (BOOL)openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    platformName platForm;
    if ([sourceApplication isEqualToString:@"com.sina.weibo"]||[sourceApplication isEqualToString:@"com.sina.weibohd"]) {
        platForm = platformSina;
    }
    else if ([sourceApplication isEqualToString:@"com.tencent.mqq"]||[sourceApplication isEqualToString:@"com.tencent.mipadqq"])
    {
        platForm = platformQQ;
    }
    else if ([sourceApplication isEqualToString:@"com.tencent.xin"]) {
        platForm = platformWeiXin;
    }
    else if ([sourceApplication isEqualToString:@"com.facebook.Facebook"]) {
        platForm = platformFaceBook;
    }
    else if([sourceApplication isEqualToString:@"com.apple.mobilesafari"]) {
        platForm = platformFaceBook;
    }else
    {
        return NO;
    }
    
    id<socialDateSource> client = [[SocialCenter sharedInstance] clientWithPlatForm:platForm];
    return [client openURL:url sourceApplication:sourceApplication annotation:annotation];
}
@end
